package baseserverframe;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;



//------------------------------------------------------------------------------








/*******************************************************************************
 * Class contains different initialization of view, to visualize
 * work with list of rules.
 * In current version it has:
 * 1. Component to show a list of rule types and interface for adding new
 * rule.
 * 2. Component to edit different properties of selected rule.
 * 
 * @author Truewar Ashman(<a href="mailto:truewar@hotmail.ru">truewar@hotmail.ru</a>) ID = 2009
 * @version 0.1<br>
 */
public class RulesView extends view
{
	/** JScrollPane for visualizaion of rules list.*/
	private JScrollPane allrulesPanel = new JScrollPane();
	/** Component to change properties of selected rule (Rule editor).*/
	private JPanel rulePropertiesPanel = new JPanel();
	/** Flag to test if allrulesPanel was initialized */
	private boolean allRules		= false;
	/** Flag to test if rulePropertiesPanel was initialized */
	private boolean ruleProperties	= false;
	/**
	 * Method fills panel with content of rules information and
	 * interfaces to edit that rules.
	 * It shows all list of assigned rules.
	 */
	public void initAllRulesPanel()
	{
		JPanel p = new JPanel();
		GridBagLayout gbl = new GridBagLayout();
		p.setLayout(gbl);

		GridBagConstraints c =  new GridBagConstraints();

		c.anchor		= GridBagConstraints.NORTH;
		c.fill			= GridBagConstraints.HORIZONTAL;
		c.gridheight	= 1;
		c.gridwidth		= GridBagConstraints.REMAINDER;
		c.gridx			= GridBagConstraints.RELATIVE;
		c.gridy			= GridBagConstraints.RELATIVE;
		c.insets		= new Insets(0, 0, 0, 0);
		c.ipadx			= 0;
		c.ipady			= 0;
		c.weightx		= 1.0;
		c.weighty		= 1.0;

		// <!TODO> ?ruleS?Table data;
		// <!TODO> ?ruleS?Iterator iter = data.getIterator();
		// <!TODO> iter.begin();
		// <!TODO> while(iter.hasNext())
		int i = 0;		// <!TOKILL>
		while(i < 30)	// <!TOKILL>
		{
			// <!TODO> iter.Next();
			// <!TODO> JLabel ruleName = new JLabel((String) iter.getLogin());
			JLabel  ruleName		= new JLabel	("Админ");
			JLabel  ruleProp		= new JLabel	("Инфа");
			JButton ruleEdit		= new JButton	("Редактировать");
			JButton ruleDelete		= new JButton	("Удалить");

			// Actions for buutons
			ruleEdit.addActionListener
			(new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					// <!TODO>
					setRuleProperties();
				}
			});

			JPanel pLine = new JPanel();
			GridLayout glLine = new GridLayout(1, 3);

			pLine.setLayout(glLine);

			pLine.add(ruleName);
			pLine.add(ruleProp);
			pLine.add(ruleEdit);
			pLine.add(ruleDelete);

			gbl.setConstraints(pLine, c);
			p.add(pLine);
			i++;	// <!TOKILL>
		}
		allrulesPanel.setViewportView(p);

		allRules = true;

	}

	/**
	 * Method fills Rule Editor with components to modify properties.
	 */
	public void initRulePropertiesPanel()
	{
		GridBagLayout gbl = new GridBagLayout();
		rulePropertiesPanel.setLayout(gbl);

		GridBagConstraints c =  new GridBagConstraints();

		c.anchor		= GridBagConstraints.NORTH;
		c.fill			= GridBagConstraints.HORIZONTAL;
		c.gridheight	= 1;
		c.gridwidth		= 2;
		c.gridx			= 1;
		c.gridy			= 0;
		c.insets		= new Insets(0, 0, 0, 0);
		c.ipadx			= 0;
		c.ipady			= 0;
		c.weightx		= 1.0;
		c.weighty		= 1.0;

		JLabel  ruleName		= new JLabel	("Админ");
		JLabel  propName1		= new JLabel	("Свойство1: ");
		JLabel  propName2		= new JLabel	("Свойство2: ");

		JTextField propVal1		= new JTextField("_");
		JTextField propVal2		= new JTextField("_");

		JButton accept			= new JButton	("Принять");
		JButton refuse			= new JButton	("Отменить");

		ruleName.setHorizontalAlignment(JLabel.CENTER);
		gbl.setConstraints(ruleName, c);
		rulePropertiesPanel.add(ruleName);

		c.gridx			= 0;
		c.gridy			= 1;
		gbl.setConstraints(propName1, c);
		rulePropertiesPanel.add(propName1);
		c.gridy			= 2;
		gbl.setConstraints(propName2, c);
		rulePropertiesPanel.add(propName2);

		c.gridx			= 2;
		c.gridy			= 1;
		gbl.setConstraints(propVal1, c);
		rulePropertiesPanel.add(propVal1);
		c.gridy			= 2;
		gbl.setConstraints(propVal2, c);
		rulePropertiesPanel.add(propVal2);

		c.gridx			= 0;
		c.gridy			= 3;
		c.fill			= GridBagConstraints.NONE;
		gbl.setConstraints(accept, c);
		rulePropertiesPanel.add(accept);

		c.gridx			= 2;
		gbl.setConstraints(refuse, c);
		rulePropertiesPanel.add(refuse);

		// Actions
		refuse.addActionListener
			(new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					// <!TODO>
					setListOfRules();
				}
			});
		ruleProperties = true;

	}

	/**
	 * It just calls another initialization methods:
	 * 1. initAllrulesPanel
	 * 2. initrulePropertiesPanel
	 */
	public void initAll()
	{
		initAllRulesPanel();
		initRulePropertiesPanel();
	}

	/**
	 * Set list of rules as current view.
	 */
	public void setListOfRules()
	{
		if(this.allRules != false)
			this.setView(this.allrulesPanel);
		else
		{
			this.initAllRulesPanel();
			this.setView(this.allrulesPanel);
		}
		if	((this.getDocLink() == null)||
			 (this.getDocLink().getFrameLink() == null))
			return;

		this.getDocLink().getFrameLink().clear();
		this.getDocLink().getFrameLink().init();
	}

	/**
	 * Set rule editor as current view.
	 */
	public void setRuleProperties()
	{
		if(this.ruleProperties != false)
			this.setView(this.rulePropertiesPanel);
		else
		{
			this.initRulePropertiesPanel();
			this.setView(this.rulePropertiesPanel);
		}
		if	((this.getDocLink() == null)||
			 (this.getDocLink().getFrameLink() == null))
			return;

		this.getDocLink().getFrameLink().clear();
		this.getDocLink().getFrameLink().init();
	}
	/**
	 * Method to add hotbars for view.
	 * There are 1 hotbar now avaible:
	 * 1. Create new rules hotbar
	 */
	public void addHotBars()
	{
		JButton addNewrule = new JButton("Добавить роль");
		this.addHB("Addrule", addNewrule);
	}
}
